//
//  SinaTabBar.swift
//  SinaWeibo
//
//  Created by 王朝阳 on 2016/10/22.
//  Copyright © 2016年 wangzhaoyang. All rights reserved.
//

import UIKit

class SinaTabBar: UITabBar {

    //MARK: - 定义闭包
    //1.创建闭包
    var closure: (()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
//        super.init(coder: aDecoder)
    }
    //MARK: - 创建视图
    private func setupUI() {
        
        //添加 kongian
        addSubview(composeButton)
    }
    
    //MARK: - 点击事件
    @objc private func buttonClick() {
//        print("点击按钮")
        closure?()
    }
    //MARK: - 设置约束
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //按钮的宽度
        let w = SinaScreenW * 0.2
        var index: CGFloat = 0
        
        //遍历 subviews
        for value in subviews {
            
            //判断是否是我们想要的UITabBarButton
            if value.isKind(of: NSClassFromString("UITabBarButton")!) {
                
                //设置宽 和 x
                value.frame.size.width = w
                value.frame.origin.x = index * w
                
                index += 1
                
            }
            //当 index == 2 的时候,让 index 加1
            if index == 2 {
                index += 1
            }
        }
        
        //设置约束
        composeButton.center.x = SinaScreenW * 0.5
        composeButton.center.y = frame.size.height * 0.5
    }
    //MARK: - 懒加载控件
    private lazy var composeButton: UIButton = {
       //创建按钮
        let button = UIButton(setImgName: "tabbar_compose_icon_add", setBackImgName: "tabbar_compose_button", target: self, action: #selector(buttonClick))
        
//        //添加点击事件
//       button.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
//        //设置背景图片
//        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: UIControlState.normal)
//        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
//        //设置图片
//        button.setImage(UIImage(named: "tabbar_compose_icon_add"), for: UIControlState.normal)
//        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
//        //设置按钮的大小,注意一定要设置按钮的大小,否则,按钮显示不出来
//        button.sizeToFit()
        return button
    }()
    
    
}
